package com.eduhzy.ttw.clazz.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ObjectUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.clazz.R;
import com.eduhzy.ttw.clazz.R2;
import com.eduhzy.ttw.clazz.di.component.DaggerHomeComponent;
import com.eduhzy.ttw.clazz.di.module.HomeModule;
import com.eduhzy.ttw.clazz.mvp.contract.HomeContract;
import com.eduhzy.ttw.clazz.mvp.model.entity.HomeData;
import com.eduhzy.ttw.clazz.mvp.presenter.HomePresenter;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;
import com.umeng.analytics.MobclickAgent;

import org.simple.eventbus.Subscriber;

import javax.inject.Inject;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-12-4 9:17:43
 * 课堂表现首页
 */
@Route(path = RouterHub.CLAZZ_HOMEACTIVITY)
public class ClazzHomeActivity extends BaseActivity<HomePresenter> implements HomeContract.View {
    @Inject
    BaseQuickAdapter<HomeData, AutoBaseViewHolder> mAdapter;
    @Inject
    RecyclerView.LayoutManager mLayoutManager;
    @BindView(R2.id.rv_list)
    RecyclerView mRvList;
    @BindView(R2.id.swipeLayout)
    SwipeRefreshLayout mSwipeLayout;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        //如找不到该类,请编译一下项目
        DaggerHomeComponent
                .builder()
                .appComponent(appComponent)
                .homeModule(new HomeModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
        MobclickAgent.onEvent(getActivity(), "ttw_class_review_home");
        return R.layout.clazz_activity_home;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(mLayoutManager);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            HomeData homeData = mAdapter.getData().get(position);
            ARouter.getInstance().build(RouterHub.CLAZZ_CLAZZREVIEWACTIVITY)
                    .withString(Intent.EXTRA_TITLE, homeData.getClassName())
                    .withParcelable(Constants.PARCELABLE_DATA, homeData)
                    .navigation(getActivity());
        });
        mSwipeLayout.setOnRefreshListener(() -> {
            getData();
        });
        getData();
    }

    private void getData() {
        mPresenter.getClassList();
    }


    @Override
    public void showLoading() {
        mSwipeLayout.setRefreshing(true);
    }

    @Override
    public void hideLoading() {
        mSwipeLayout.setRefreshing(false);
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Subscriber(tag = RouterHub.CLAZZ_HOMEACTIVITY)
    public void onEvents(Message msg) {
        switch (msg.what) {
            case EventBusTags.REVIEW_UPDATE:
                getData();
                break;
            default:
        }
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void setEmptyView() {
        if (ObjectUtils.isNotEmpty(mAdapter) && mAdapter.getEmptyViewCount() <= 0) {
            View view = View.inflate(getActivity(), R.layout.public_custom_empty_layout, null);
            ImageView iv = view.findViewById(R.id.iv_logo);
            iv.setImageResource(R.drawable.no_class);
            QMUIRoundButton btn = view.findViewById(R.id.btn_submit);
            btn.setText("加入班级");
            btn.setOnClickListener((view1) -> {
                ARouter.getInstance().build(RouterHub.APP_JOINCLASSACTIVITY)
                        .withString(Intent.EXTRA_TITLE, Constants.JOIN_CLASS)
                        .navigation();
            });
            mAdapter.setEmptyView(view);
        }
    }
}
